package cn.acewill.yxd.yunxundian.ui.follow_up;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.acewill.yxd.yunxundian.BaseActivity;
import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.pubClass.MyFragmentAdapter;

@ContentView(R.layout.activity_follow_up)
public class FollowUpActivity extends BaseActivity {
    public static final String TAG = "FollowUpActivity";
    public static final String []sTitle = new String[]{"未完成","待确认","已完成"};
    @ViewInject(R.id.toolbar)
    private Toolbar toolbar;
    @ViewInject(R.id.container)
    private ViewPager mViewPager;
    @ViewInject(R.id.tabs)
    private TabLayout mTabLayout;
    @ViewInject(R.id.textTitle)
    private TextView mTitle;
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        //Log.i("FollowUpActivity","onCreate");
        mTitle.setText("问题跟踪");
        setSupportActionBar(toolbar);
        if(getSupportActionBar() != null){
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

        initView();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        //Log.i("FollowUpActivity", "onRestart");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_searchview, menu);
        MenuItem menuItem = menu.findItem(R.id.searchView);
        SearchView mSearchView = (SearchView) menuItem.getActionView();
        mSearchView.setQueryHint("搜索评估名或截止日期");
        //mSearchView.setIconified(false);
        //mSearchView.setIconifiedByDefault(false);
        //mSearchView.onActionViewExpanded();
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                //Log.i("onQueryTextSubmit","TextSubmit : " + query);
                for(Fragment fragment:fragments){
                    FollowupListFragment followupListFragment = (FollowupListFragment)fragment;
                    if(followupListFragment!=null && followupListFragment.fumAdapter!=null)
                        followupListFragment.fumAdapter.getFilter().filter(query);
                }
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                //Log.i("onQueryTextChange","TextChange --> " + newText);
                if(TextUtils.isEmpty(newText)){
                    for(Fragment fragment:fragments){
                        FollowupListFragment followupListFragment = (FollowupListFragment)fragment;
                        if(followupListFragment!=null && followupListFragment.fumAdapter!=null)
                            followupListFragment.fumAdapter.getFilter().filter(newText);
                    }
                }
                return false;
            }
        });
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                // If we got here, the user's action was not recognized.
                // Invoke the superclass to handle it.
                return super.onOptionsItemSelected(item);
        }
    }

    private void initView() {
        for(String title:sTitle){
            mTabLayout.addTab(mTabLayout.newTab().setText(title));
        }

/*
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Log.i(TAG,"onTabSelected:"+tab.getText());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        */
        mTabLayout.setupWithViewPager(mViewPager);
        for(int i=FollowupListFragment.FOLLOW_UP_MAIN;i<=FollowupListFragment.FOLLOW_UP_HISTORY;i++){
            fragments.add(FollowupListFragment.newInstance(i));
        }

        MyFragmentAdapter adapter = new MyFragmentAdapter(getSupportFragmentManager(),fragments, Arrays.asList(sTitle));
        mViewPager.setAdapter(adapter);
/*
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.i(TAG,"select page:"+position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });*/

    }
}
